package cn.wsalix.observable.service.impl;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import cn.wsalix.admin.entity.SysMenu;
import cn.wsalix.admin.form.MenuForm;
import cn.wsalix.admin.status.OperateEnum;
import cn.wsalix.observable.service.MenuChangeService;

public class MenuChangeServiceImpl extends Observable implements
		MenuChangeService {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	private SysMenu menu;
	private List<? extends Observer> serviceList;

	@PostConstruct
	public void init() {
		if (serviceList != null) {
			for (Observer obj : serviceList) {
				addObserver(obj);
			}
		}
		logger.info("==========init");
		/*
		 * addObserver(userCenterService); addObserver(userInfoService);
		 * addObserver(userService);
		 */
	}

	@PreDestroy
	public void destroy() {
		logger.info("==========destroy");
	}

	public List<? extends Observer> getServiceList() {
		return serviceList;
	}

	public void setServiceList(List<? extends Observer> serviceList) {
		this.serviceList = serviceList;
	}

	@Transactional
	@Override
	public SysMenu add(MenuForm menuForm) {
		menuForm.setOperate(OperateEnum.add);
		setChanged();
		notifyObservers(menuForm);
		return menu;
	}

	@Transactional
	@Override
	public SysMenu edit(MenuForm menuForm) {
		menuForm.setOperate(OperateEnum.edit);
		setChanged();
		notifyObservers(menuForm);
		return menu;
	}

	@Transactional
	@Override
	public SysMenu delete(MenuForm menuForm) {
		menuForm.setOperate(OperateEnum.del);
		setChanged();
		notifyObservers(menuForm);
		return menu;
	}

	@Override
	public SysMenu getMenu() {
		return menu;
	}

	@Override
	public void setMenu(SysMenu menu) {
		this.menu = menu;
	}

}
